import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hixing_app/app_inject.dart';
import 'package:hixing_app/router/router.dart';
import 'package:hixing_app/utils/device_info.dart';
import 'package:hixing_app/utils/screen_ui.dart';
import 'package:hixing_app/views/debug/debug_view.dart';
import 'package:hixing_app/views/not_found/not_found.dart';
import 'package:hixing_app/views/splash/splash.dart';
import 'package:hixing_app/widgets/toast/toast.dart';

import 'const/app_config.dart';

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    ScreenUI.initScreenUI(context);
    DeviceUtils.getDevHardWare();
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return GetMaterialApp(
            title: 'Uxon App',
            // 初始化注入依赖
            initialBinding: AppInjector(),
            // 定义统一的路由过渡动画
            defaultTransition: Transition.rightToLeft,
            transitionDuration: AppConfig.routerTransitionDuration,
            navigatorObservers: [BotToastNavigatorObserver()],
            // theme: Themes.light,
            // translations: LanguageSource(),

            // 默认展示本地语言
            locale: const Locale('zh', 'CN'),
            // 备用语言
            fallbackLocale: const Locale('en', 'US'),
            routingCallback: (routing) {
              // 如果 该路由需要登录，且没token 跳转登录页面
              // if (indexRouter.authRouters.contains(routing?.current) &&
              //     hasNotToken) {
              //   return LoginPage();
              // }
              // logs('当前路由页面${routing?.current}');
            },
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: [
              const Locale('zh', 'CN'),
              const Locale('en', 'US'),
            ],
            home: SplashPage(),
            builder: (BuildContext ctx, Widget? widget) {
              Widget current = widget!;
              // if (!BaseUrlUtils.isRelease) {
              // if (true) {
              current = Stack(
                children: [
                  widget,
                  const DebugView(),
                ],
              );
              // }
              current = GestureDetector(
                onTap: () {
                  /// app 全局处理键盘关闭
                  final FocusScopeNode currentFocus = FocusScope.of(context);
                  if (!currentFocus.hasPrimaryFocus &&
                      currentFocus.focusedChild != null) {
                    FocusManager.instance.primaryFocus?.unfocus();
                  }
                },
                child: current,
              );

              // 初始化 toast
              return toastInit()(context, current);
            },
            unknownRoute:
                GetPage(name: IndexRouter.notFound, page: () => NotFoundPage()),
            getPages: IndexRouter.genRouter);
      },
    );
  }
}
